const { Controller } = require('egg')
const Response = require('../utils/Response')
/**
 * @Controller  用户相关模块
 */
class UserControll extends Controller {
  /**
   * @Router post /login
   * @Summary 用户登录
   * @Description account 和 password 为必传项
   * @Request body login
   */
  async login() {
    let { ctx, service } = this
    let user = ctx.request.body
    // console.log(user);
    let res = await service.user.login(user)
    // console.log(res);
    if (res.message == "登录成功") {
      // 签发token
      // 这里token的有效时长为1小时
      let token = 'Bearer ' + this.app.jwt.sign(
        { username: user.account },
        this.app.config.jwt.secret,
        { expiresIn: "3600s" }
      )
      ctx.body = new Response(200, { token, accountInfo: res.accountInfo, user: res.user }, "操作成功")
    } else {
      ctx.body = new Response(403, "账号或密码错误", "操作失败")
    }
  }
  /**
   * @Router post /register
   * @Summary 用户注册
   * @Description userId、account、password、roleId、mobile 为必传项
   * @Request body register
   */
  async register() {
    let { ctx, service } = this
    let user = ctx.request.body
    // console.log(user);
    let res = await service.user.register(user)
    if (res == "注册成功") {
      ctx.body = new Response(200, res, "操作成功")
    } else {
      ctx.body = new Response(403, res, "操作失败")
    }
  }

  // 重置用户密码
  /**
 * @Router post /user/resetPassword
 * @Summary 重置用户密码
 * @Description userId 为必传项
 * @Request body resetPsd
 * @apikey
 */
  async resetPassword() {
    let { ctx, service } = this
    let { userId } = ctx.request.body
    if (!userId) {
      ctx.body = new Response(400, "用户id和新密码不能为空", "操作失败")
    } else {
      let res = await service.user.resetPassword(userId)
      if (res == "重置成功") {
        ctx.body = new Response(200, res, "操作成功")
      } else {
        ctx.body = new Response(403, res, "操作失败")
      }
    }
  }
  // 用户信息修改
  /**
* @Router post /user/updateUserInfo
* @Summary 更新用户信息
* @Description userId 为必传项
* @Request body updateUser
* @apikey
*/
  async updateUserInfo() {
    let { ctx, service } = this
    let userInfo = ctx.request.body
    let res = await service.user.updateUserInfo(userInfo)
    if (res == "更新成功") {
      ctx.body = new Response(200, res, "操作成功")
    } else {
      ctx.body = new Response(403, res, "操作失败")
    }
  }
  // 更新用户密码
  /**
* @Router post /user/updatePassword
* @Summary 更新用户密码
* @Description userId、password 为必传项
* @Request body updatePassword
* @apikey
*/
  async updatePassword() {
    let { ctx, service } = this
    let { userId, password,newpsd } = ctx.request.body
    if (!userId || !password) {
      ctx.body = new Response(200, "用户id或密码不能为空", "操作成功")
    } else {
      let res = await service.user.updatePassword(userId, password,newpsd)

      if (res == "修改成功") {
        ctx.body = new Response(200, res, "操作成功")
      } else {
        ctx.body = new Response(403, res, "操作失败")
      }
    }

  }
  //  单个添加基础用户
    /**
* @Router post /user/addBaseUsers
* @Summary 单个添加基础用户
* @Description userId,userName, age,gender ,roleId ,type(1为添加2为更新）为必传项 
* @Request body addBUser
* @apikey
*/
  async addBaseUsers(){
    let { ctx, service } = this
    let { userId,userName, age,gender ,roleId,type} = ctx.request.body

    let result = await service.user.addBaseUsers(userId,userName, age,gender,roleId,type)
    ctx.body = new Response(200,result,"")
  }

  // 获取用户收藏的实验室列表
  // 条件查询 实验室名称(暂时就有个实验室名称的模糊搜索)
  /**
* @Router get /user/getUserCollect
* @Summary 获取用户实验室收藏列表
* @Description userId 为必传项
* @Request query string *userId
* @Request query string labName
* @Request query number typeId
* @apikey
*/
  async getUserCollect() {
    let { ctx, service } = this
    let { userId, labName, typeId } = ctx.query
    if (!labName) {
      labName = ""
    }
    if (!userId) {
      ctx.body = new Response(400, "userId不能为空", "")
    } else {
      let res = await service.user.getUserCollect(userId, labName, typeId)
      ctx.body = new Response(200, res, "")
    }

  }
  // 校验账号唯一性
  /**
 * @Router get /checkoutAccountOnly
 * @Summary 检验账号唯一性
 * @Description account 为必传项
 * @Request query string *account
 */
  async checkoutAccountOnly() {
    let { ctx, service } = this
    let { account } = ctx.query
    let res = await service.user.checkoutAccountOnly(account)
    // console.log(res);
    ctx.body = new Response(200, res, "操作成功")
  }
  // 获取用户全部信息
  /**
* @Router get /user/getUserInfo
* @Summary 获取用户信息详情
* @Description id(userId) 、roleId 为必传项
* @Request query string *id
* @Request query number *roleId
* @apikey
*/
  async getUserInfo() {
    let { ctx, service } = this
    let { id, roleId } = ctx.query
    let res = await service.user.getUserInfo(id, roleId)
    // console.log(res);
    if (!id || !roleId) {
      this.ctx.body = new Response(400, "id或roleId不能为空", "操作失败")
    } else if (res.message == "信息查询成功") {
      this.ctx.body = new Response(200, res, "操作成功")
    } else {
      this.ctx.body = new Response(403, res, "操作失败")
    }
  }

  // 获取用户预约列表
  /**
* @Router get /user/getUserRes
* @Summary 获取用户预约列表
* @Description userId 为必传项
* @Request query string *userId
* @Request query number status
* @Request query string labName
* @Request query string dateTime
* @Request query number reservationId
* @apikey
*/
  async getUserRes() {
    let { service, ctx } = this
    let { userId,status,labName,dateTime,reservationId,type} = ctx.query
    if (!userId) {
      ctx.body = new Response(400, "缺少必填字段userId", "")
    } else {
      let result = await service.user.getUserRes(userId,status,labName,dateTime,reservationId,type)
      ctx.body = new Response(200, result, "")
    }
  }
  /**
* @Router get /user/getUsers
* @Summary 条件获取用户列表
* @Description roleId为必传项
* @Request query number *roleId
* @Request query string userName
* @Request query string userId
* @Request query number searchRole
* @apikey
*/
  async getUsers(){
    let { service, ctx } = this
    let { userId,userName,roleId,searchRole} = ctx.query
    let res = await service.user.getUsers(userId,userName,roleId,searchRole)
    ctx.body = new Response(200,res,"")
  }

  //  条件查询出基础用户数据
    /**
* @Router get /user/getBaseUsers
* @Summary 条件获取基础用户数据
* @Description searchRole非必传，传默认查询出全部
* @Request query number searchRole
* @Request query string userName
* @Request query string userId
* @apikey
*/
  async getBaseUsers(){
    let { service, ctx } = this
    let {searchRole,userName,userId} = ctx.query
    let res = await service.user.getBaseUsers(searchRole,userName,userId)
    ctx.body = new Response(200,res,"")
  }

  // 根据id删除用户
      /**
* @Router delete /user/deleteUser
* @Summary 根据用户id删除用户
* @Description userId为必传项
* @Request query string *userId
* @apikey
*/
 async deleteUser(){
  let { service, ctx } = this
  let {userId} = ctx.request.body
  let result = await service.user.deleteUser(userId)
  ctx.body = new Response(200,result,"")
 }
}
module.exports = UserControll